﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ECS.DAL.Component;
using ECS.DAL.Entity;
using ECS.Framework;
namespace E_ICS.Master
{
    public partial class frmZoneLocation : Form
    {
        #region private properties
        private ZoneLocationComponent _dataComponent = new ZoneLocationComponent();
        private SortableBindingList<ZoneLocation> _gridSource = new SortableBindingList<ZoneLocation>();
        private List<Zone> _comboLocation = new List<Zone>();
        List<ObjectContext> _deleteList = new List<ObjectContext>();
        #endregion

        #region form event
        public frmZoneLocation()
        {
            InitializeComponent();
        }

        private void frmZoneLocation_Load(object sender, EventArgs e)
        {
            List<ObjectContext> _zoneList = new List<ObjectContext>();
            try
            {
                SetScreen();
                LoadZoneCombobox();
                dgvZone.AutoGenerateColumns = false;
                _zoneList = _dataComponent.SearchUnCouple();
                foreach (ObjectContext item in _zoneList)
                {
                    //item.NotChangeObject();
                    _gridSource.Add((ZoneLocation)item);
                }
                SetGridData();
                
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        
        
        private void SetScreen()
        {
            this.Text = CommonResourceString.GetResourseValue("scn_Zone_Location_Setup_frm_name");
            btnSave.Text = CommonResourceString.GetResourseValue("scn_Zone_Location_Setup_frm_btnSave");
            btnClose.Text = CommonResourceString.GetResourseValue("scn_Zone_Setup_frm_btnClose");
                  
          
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmZoneLocation_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result;
            try
            {
                result = CheckSaves();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }

        private void dgvZone_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (_gridSource.Count > 0)
                {
                    if (e.ColumnIndex == dgvZone.Columns["btnDelete"].Index && e.RowIndex >= 0)    // Delete Dim
                    {
                        if (CommonMessageBox.ShowMessageConfirmDelete() == DialogResult.Yes)
                        {
                            if (_gridSource[e.RowIndex].GetObjectSate() != OBJECT_STATE.Insert)
                            {
                                ZoneLocation obj = _gridSource[e.RowIndex];
                                obj.AppId = GlobalVariable.user.ApplicationID;
                                obj.UserId = GlobalVariable.user.UserID;
                                obj.Device = GlobalVariable.user.Device;
                                obj.DeleteObject();
                                _dataComponent.Delete((ObjectContext)_gridSource[e.RowIndex]);
                            }
                            _gridSource.RemoveAt(e.RowIndex);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
        #endregion

        #region private methods
        private void LoadZoneCombobox()
        {
            ZoneComponent zone = new ZoneComponent();
            List<ObjectContext> data = zone.Search(null);
            foreach (Zone item in data)
            {
                _comboLocation.Add((Zone)item);
            }
        }
        /// <summary>
        /// set grid data source
        /// </summary>
        private void SetGridData()
        {
            dgvZone.DataSource = _gridSource;
            DataGridViewComboBoxColumn customCombo = (DataGridViewComboBoxColumn)dgvZone.Columns[1];
            customCombo.Items.Clear();
            customCombo.Items.Add("");
            foreach(Zone item in _comboLocation)
            {
                customCombo.Items.Add(item.ZoneName);
            }
        }
        /// <summary>
        /// Check data in grid which are changed then save change
        /// </summary>
        /// <returns></returns>
        private DialogResult CheckSaves()
        {
            bool isChange = false;
            DialogResult result = System.Windows.Forms.DialogResult.No;
            List<Zone> selectZone = new List<Zone>();
            foreach (ZoneLocation item in _gridSource)
            {
                if (item.ObjectStatus != OBJECT_STATE.None)
                {
                    isChange = true;
                    break;
                }
            }
            if (_deleteList.Count > 0)
            {
                isChange = true;
            }
            if(isChange)
            {
                result = CommonMessageBox.displayConfirmSave();
                if (result == System.Windows.Forms.DialogResult.Yes)
                {
                    foreach (ZoneLocation data in _gridSource)
                    {
                        if (data.ObjectStatus != OBJECT_STATE.None)
                        {
                            data.AppId = GlobalVariable.user.ApplicationID;
                            data.Device = GlobalVariable.user.Device;
                            data.UserId = GlobalVariable.user.UserID;
                            if (!string.IsNullOrEmpty(data.ZoneName))
                            {
                                //var selectZone 
                                selectZone = _comboLocation.Where(o => o.ZoneName == data.ZoneName).ToList();
                                if (selectZone != null && selectZone.Count > 0)
                                {
                                    data.ZoneId = selectZone[0].ZoneId;
                                }
                                else
                                {
                                    data.ObjectStatus = OBJECT_STATE.None;
                                }
                            }
                            else
                            {
                                if (data.GetObjectSate() == OBJECT_STATE.Update)
                                {
                                    data.ZoneId = string.Empty;
                                }
                                else
                                {
                                    data.ObjectStatus = OBJECT_STATE.None;
                                }
                            }
                        }
                        //if (data.ObjectStatus != OBJECT_STATE.None)
                        //{
                        //    var selectZone = _comboLocation.Where(o => o.ZoneName == data.ZoneName).ToList();
                        //    if (selectZone != null && selectZone.Count > 0)
                        //    {
                        //        data.ZoneId = selectZone[0].ZoneId;
                        //    }
                        //}
                    }
                    _dataComponent.SaveChanges(_gridSource.ToList<ObjectContext>());
                    _dataComponent.SaveChanges(_deleteList);
                    _deleteList.Clear();
                }
            }
            
            return result;
        }
        #endregion
    }
}
